/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-12 15:07:41
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-11-05 15:37:18
 */
import {
  dictList,
  dictStoreList,
  dictProvinceList,
  dictCityList,
  dictInstitutionList,
  dictUpload,
  dictStoreUnavailableTimeList,
  dictOrderPackageStoreList,
} from 'services/dict';

export default {
  namespace: 'dict',
  state: {
    dictList: [],
    storeList: [],
    institutionList: [],
    provinceList: [],
    cityList: [],
    storeUnavailableTimeList: [],
    dictOrderPackageStoreList: [],
  },

  effects: {
    // 通用字段表
    *dictList({ payload, callback }, { call, put }) {
      const response = yield call(dictList, payload);
      yield put({
        type: 'saveDictList',
        payload: response,
      });
    },
    // 门店列表
    *dictStoreList({ payload, callback }, { call, put }) {
      const response = yield call(dictStoreList, payload);
      yield put({
        type: 'saveStoreList',
        payload: response,
      });
    },
    // 清除门店列表
    *dictClearStoreList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveClearStoreList',
      });
    },

    // 体检机构列表
    *dictInstitutionList({ payload, callback }, { call, put }) {
      const response = yield call(dictInstitutionList, payload);
      yield put({
        type: 'saveInstitutionList',
        payload: response,
      });
    },
    // 省份列表
    *dictProvinceList({ payload, callback }, { call, put }) {
      const response = yield call(dictProvinceList, payload);
      yield put({
        type: 'saveProvinceList',
        payload: response,
      });
    },
    // 城市列表
    *dictCityList({ payload, callback }, { call, put }) {
      const response = yield call(dictCityList, payload);
      yield put({
        type: 'saveCityList',
        payload: response,
      });
    },
    // 上传
    *dictUpload({ payload, callback }, { call, put }) {
      const response = yield call(dictUpload, payload);
      callback ? callback(response) : '';
    },
    // 门店不可预约时间
    *dictStoreUnavailableTimeList({ payload, callback }, { call, put }) {
      const response = yield call(dictStoreUnavailableTimeList, payload);
      yield put({
        type: 'saveDictStoreUnavailableTimeList',
        payload: response,
      });
      callback ? callback(response) : '';
    },

    // 下单套餐可选门店列表
    *dictOrderPackageStoreList({ payload, callback }, { call, put }) {
      const response = yield call(dictOrderPackageStoreList, payload);
      yield put({
        type: 'saveDictOrderPackageStoreList',
        payload: response,
      });
      callback ? callback(response) : '';
    },
  },

  reducers: {
    saveDictList(state, action) {
      const { data } = action.payload;
      const newList = data.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        v.id = `${v.id}`;
        return v;
      });
      return {
        ...state,
        dictList: newList,
      };
    },
    saveStoreList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        v.storeId = `${v.id}`;
        return v;
      });
      return {
        ...state,
        storeList: newList,
      };
    },
    saveClearStoreList(state, action) {
      return {
        ...state,
        storeList: [],
      };
    },
    saveInstitutionList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        institutionList: newList,
      };
    },
    saveProvinceList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        provinceList: newList,
      };
    },
    saveCityList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        cityList: newList,
      };
    },

    saveDictStoreUnavailableTimeList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        storeUnavailableTimeList: newList,
      };
    },

    saveDictOrderPackageStoreList(state, action) {
      const { data } = action.payload;
      const newList = data.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        dictOrderPackageStoreList: newList,
      };
    },

    // end
  },
};
